'use strict';

// 原型对象:
var Student = {
    name: 'Robot',
    height: 1.2,
    run: function () {
        console.log(this.name + ' is running...');
    }
};

var xiaoming = {
    name: '小明'
};

xiaoming.__proto__ = Student;

console.log(xiaoming.run());

//在编写JavaScript代码时，不要直接用obj.__proto__去改变一个对象的原型，并且，低版本的IE也无法使用__proto__
function createStudent(name) {
    // 基于Student原型创建一个新对象:
    var s = Object.create(Student);
    // 初始化新对象:
    s.name = name;
    return s;
}

var xiaoming = createStudent('小明');
xiaoming.run(); // 小明 is running...
xiaoming.__proto__ === Student; // true

console.log(xiaoming.height);

function Boss(name) {
    this.name = name;
    this.hello = function () {
        return 'wo ai niu de hua';
    }
}

var xiaoming = new Boss('小明');
xiaoming.name; // '小明'
xiaoming.hello(); // Hello, 小明!
console.log(xiaoming.hello());

//用new Student()创建的对象还从原型上获得了一个constructor属性，它指向函数Student本身
//xiaoming.constructor === Student.prototype.constructor; // true
//Student.prototype.constructor === Student; // true
//Object.getPrototypeOf(xiaoming) === Student.prototype; // true
//xiaoming instanceof Student; // true

//原型链如下：
//xiaoming ----> Student.prototype ----> Object.prototype ----> null
function Women(name) {
    this.name = name;
}

Women.prototype.hello = function () {
    alert('Hello, ' + this.name + '!');
};

//请利用构造函数定义Cat，并让所有的Cat对象有一个name属性，并共享一个方法say()，返回字符串'Hello, xxx!'：
function Cat(name) {
    this.name = name;
}

//共享就该这么写
Cat.prototype.say = function() {
    return `Hello, ${this.name}!` ;
}


var mimi = new Cat('kitty');
console.log(mimi.say());

//如果用新的class关键字来编写Dog，可以这样写
class Dog {
    constructor(name) {
        this.name = name;
    }

    feature() {
        //alert('Hello, ' + this.name + '!');
        console.log('我是laopaoer');
    }
}

//最后，创建一个Student对象代码和前面章节完全一样：
var dudu = new Dog('laopaoer');
dudu.feature();

//class继承，用class的好处就是极大地简化了原型链代码
class PrimaryStudent extends Student {
    constructor(name, grade) {
        super(name); // 记得用super调用父类的构造方法!
        this.grade = grade;
    }

    myGrade() {
        alert('I am at grade ' + this.grade);
    }
}





















